<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>

    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }



        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 150%;
            max-width: 150%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 110%;
            padding-left: 10px;

        }



        .datatable td, .datatable th {
            background: #EFEFF4 !important;
            padding: 12px;
            min-width: 50px;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: center;

        }


    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理
            <small>预约列表</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>预约列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="档案号/姓名/手机号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                <el-button type="danger" class="butT" @click="cancelSelected()">批量取消</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row
                      @selection-change="handleSelectionChange">
                <el-table-column type="selection" :selectable="checkSelectable"></el-table-column>
                <el-table-column prop="orderDate" label="预约日期" align="center"></el-table-column>
                <el-table-column prop="fileNumber" label="档案号" align="center"></el-table-column>
                <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                <el-table-column prop="phoneNumber" label="手机号码" align="center"></el-table-column>
                <el-table-column label="预约类型" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.orderType == '微信预约' ? '微信预约' : '电话预约'}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="预约状态" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.orderStatus == '未到诊'?'未到诊':scope.row.orderStatus =='已确认未到诊' ? '已确认未到诊':'已就诊'}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">

                        <el-tooltip class="item" effect="dark" content="确认预约" placement="top-start">
                        <el-button
                                icon="el-icon-success"
                                    type="primary"
                                   size="mini"
                                   :disabled="scope.row.orderStatus =='已确认未到诊'||scope.row.orderStatus =='已取消'||scope.row.orderStatus =='已到诊'?true:false"
                                   @click="confirmOrder(scope.row)">
                        </el-button>
                        </el-tooltip>

                        <el-tooltip class="item" effect="dark" content="取消预约" placement="top-start">
                        <el-button
                                    icon="el-icon-error"
                                    type="danger"
                                    size="mini"
                                   @click="cancelOrder(scope.row)">
                        </el-button>
                        </el-tooltip>


                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增预约" :visible.sync="dialogFormVisible" @close="empty">
                    <el-form label-position="right" label-width="100px">
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form :model="formData" ref="dataAddForm" :rules="rules" label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="姓名" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="身份证" prop="idCard">
                                                <el-input v-model="formData.idCard"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <el-date-picker
                                                        v-model="formData.birthday"
                                                        type="date"
                                                        placeholder="选择日期"
                                                        :picker-options="pickerOptions1"
                                                >
                                                </el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="手机号" prop="phoneNumber">
                                                <el-input v-model="formData.phoneNumber"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="年龄" prop="age">
                                                <el-input v-model="formData.age"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="婚姻状态">
                                                <el-select v-model="formData.maritalsStatus">
                                                    <el-option label="未婚" value="1"></el-option>
                                                    <el-option label="已婚" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="预约时间" prop="orderDate">
                                                <el-date-picker
                                                        value-format="yyyy-MM-dd"
                                                        v-model="formData.orderDate"
                                                        type="date"
                                                        :picker-options="pickerOptions2"
                                                        placeholder="选择日期">
                                                </el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>

                            <el-tab-pane label="套餐信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>

                                        </tr>
                                        <thead class="aq_thead">
                                        <tr>
                                            <th width="60">选择</th>
                                            <th>套餐名称</th>
                                            <th width="80">适用人群</th>
                                            <th>套餐介绍</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="setmealIds" type="checkbox"
                                                       :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.sex=='0'?'不限':c.sex=='1'?'男':'女'}}</label>
                                            </td>
                                            <td><label :for="c.id">{{c.remark}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>

                        </el-tabs>
                    </el-form>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="empty()">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//添加表单窗口中检查组列表数据
            setmealIds: [],//添加表单窗口中检查组复选框对应id
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            rules: {//校验规则
                name: [{required: true, message: '请输入姓名', trigger: 'blur'}],
                phoneNumber: [
                    {required: true, message: '请输入手机号', trigger: 'blur'},
                    {
                        pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/,
                        message: '手机号格式不对',
                        trigger: 'blur'
                    }],
                age: [{required: true, message: '请输入年龄', trigger: 'blur'}],
                idCard: [{required: true, message: '请输入年龄', trigger: 'blur'}]
            },
            pickerOptions2: {
                disabledDate(date) {
                    return date.getTime() <= Date.now();
                }
            },
            pickerOptions1: {
                disabledDate(date) {
                    return date.getTime() > Date.now();
                }
            },

            orders: [],//用于接收被选择的复选框,
            flag:false

        },
        created() {
            this.findPage();
        },
        methods: {

            //分页查询
            findPage() {
                if(this.pagination.queryString == "" && this.pagination.currentPage > 1){
                    this.pagination.queryString = null;
                    this.pagination.currentPage = 1;
                }
                //获取分页相关数据
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString

                };
                //发送ajax请求
                axios.post("/order/pageQuery.do", param).then((res) => {
                    //解析响应数据，并为模型数据赋值
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                    // this.pagination.currentPage=this.dataList[0].currentPage;
                    if (this.pagination.queryString!=null && this.pagination.queryString.length!=0) {
                        this.pagination.currentPage=res.data.rows[0].currentPage;

                    }
            })

            },
            // 清空表单检验失败提示
            empty(){
                this.$refs['dataAddForm'].resetFields();
                this.dialogFormVisible = false;
                this.findPage();

            },
            //重置表单
            resetForm() {
                this.formData = {};
                this.activeName = 'first';
                this.setmealIds = [];
                this.imageUrl = null;//重置图片上传的url

            },

            //确认预约之后就直接发送短信给用户
            confirmOrder(row) {
                this.$confirm("你确定要进行预约吗？", "提示", {//确认框
                    type: 'success'
                }).then(() => {

                    var param = {
                        phoneNumber: row.phoneNumber,
                        name: row.name,
                        orderDate: row.orderDate,
                        id: row.id
                    };
                    //点击确定按钮，发送ajax请求，确认
                    axios.post("/order/confirm.do", param).then((res) => {
                        if (res.data.flag) {
                            //执行成功
                            //弹出成功提示信息
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                            this.findPage();
                        } else {
                            //执行失败
                            this.$message.error(res.data.message);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    });
                }).finally(() => {
                    this.findPage();
                });

            },

            /*取消预约：业务逻辑：1.取消预约后，提示是否确认取消
                                2.确认取消后，将预约状态变为已取消，按钮都置灰，无法再点击
                                3.发送短信告知用户已取消预约；
                                4.无论取消与否都在查询一次
            */
            cancelOrder(row) {
                //1.弹出提示框，提示是否确认取消预约
                var param = {
                    orderDate: row.orderDate,
                    phoneNumber: row.phoneNumber,
                    name: row.name
                };
                this.$confirm('确认取消预约吗？', {
                    type: 'warning',

                }).then(() => {
                    //2.确认取消=>发送ajax请求到后台取消预约
                    axios.post("/order/cancelOrder.do?id=" + row.id, param).then((res) => {

                        //2.2 判断后台是否执行取消成功
                        if (res.data.flag) {
                            //2.2.1 执行成功给出成功取消的提示信息
                            this.$message({
                                message: '取消成功',
                                type: 'success',
                            });

                            this.findPage();
                        } else { //2.2.2 执行取消失败，给出提示
                            this.$message.error(res.data.message)
                        }
                    });

                }).finally(() => {
                    //4.无论取消与否都在查询一次
                    this.findPage();
                });

            },


            /*
            * 批量取消：
            *    业务逻辑：0.将获取的rows数据赋值给全局变量orders集合
            *            1.判断选择的行是否有已确认或者已取消的状态，有的话就直接不可勾选
            *            2.点击批量删除后将获取的orders集合传递到
            *            3.发送ajax请求到后台批量取消
            *
            * */
            //根据状态确认复选框是否可选，根据函数的返回值确定
            checkSelectable(row) {

                if (row.orderStatus == '已取消' || row.orderStatus == '已确认未到诊' || row.orderStatus == '已到诊' || row.orderDate < Date.now()) {
                    return false;
                }
                return true;
            },

            handleSelectionChange(rows) {
                this.orders = rows;

            },
            //取消已选择
            cancelSelected() {
                if(this.orders!=null&&this.orders.length>0){
                    this.$confirm('确认批量取消预约吗？', {
                        type: 'warning',

                    }).then(() => {
                        //发送ajax请求
                        axios.post("/order/cancelSelected.do", this.orders).then((res) => {
                            if (res.data.flag) {
                                this.$message({
                                    type: 'success',
                                    message: "批量取消成功",

                                });
                                this.findPage();
                            } else {
                                this.$message.error(res.data.message)
                            }
                        });

                    }).finally(() => {
                        this.findPage();
                    });
                }else {
                    this.$message.error("请先选择需要操作的预定");

                }

            },
            // 弹出添加窗口
            handleCreate() {

                //弹出新建窗口，并设置基本信息选项卡为默认展示tab
                this.dialogFormVisible = true;
                this.resetForm();
                //防止预定表分页查询条件同步绑定到检查套餐；
                this.pagination.queryString = null;

                //定义分页模型参数
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //发送ajax请求,套餐信息分页查询
                axios.post("/setmeal/pageQuery.do", param).then((res) => {
                    //给模型数据赋值
                    this.tableData = res.data.rows;
                    this.pagination.total = res.data.total;
                });
            },
            //添加order==》业务逻辑，1.添加预定基本信息；2.关联的预定套餐
            handleAdd() {
                //0.表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        //1.发送ajax请求，提交数据到后台，需要需要写道setmeal_id
                        axios.post("/order/add.do?setmealIds=" + this.setmealIds, this.formData)
                            .then((res) => {
                                //关闭新增窗口
                                this.dialogFormVisible = false;

                                if (res.data.flag) {//执行成功给出成功提示
                                    this.$message({
                                        type: 'success',
                                        message: res.data.message
                                    })
                                    this.findPage();
                                } else {
                                    this.$message.error(res.data.message)
                                }
                            }).finally(() => {
                            this.findPage()
                        });
                    }else {
                        this.$message.error("表单数据效验失败");
                        return false;
                    }
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            }
        }
    })
</script>

</html>
